﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace SAUlot
{
    public class AirplaneView : Microsoft.Xna.Framework.Game // UserControl
    {
        // XNA STUFF
        private readonly GraphicsDeviceManager graphics;
        private SpriteBatch spriteBatch;
        private readonly AirplaneMainView form;

        private Sprite mSprite;

        private AirplaneControler airplaneControler;

        #region Settery i gettery

        public AirplaneControler Controler
        {
            get { return airplaneControler; }
            set { airplaneControler = value; }
        }
        #endregion

        // metoda, ktora powinna zostac podlaczona do eventu modelu
        public void UpdateState(object sender, double[] result)
        {
            mSprite.Update(result);
        }

        public AirplaneView(AirplaneMainView f)
        {
            form = f;
            graphics = new GraphicsDeviceManager(this)
            {
                PreferredBackBufferWidth = form.ViewportSize.Width,
                PreferredBackBufferHeight = form.ViewportSize.Height
            };
            graphics.PreparingDeviceSettings += graphics_PreparingDeviceSettings;
            System.Windows.Forms.Control.FromHandle(Window.Handle).VisibleChanged += MainGame_VisibleChanged;
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
        }

        void MainGame_VisibleChanged(object sender, System.EventArgs e)
        {
            if (System.Windows.Forms.Control.FromHandle(Window.Handle).Visible)
                System.Windows.Forms.Control.FromHandle(Window.Handle).Visible = false;
        }

        void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
        {
            e.GraphicsDeviceInformation.PresentationParameters.DeviceWindowHandle = form.CanvasHandle;
        }

        protected override void Initialize()
        {
            mSprite = new Sprite(); 
            mSprite.canvasCenter = new Vector2(form.ViewportSize.Width / 2, form.ViewportSize.Height / 2);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice); 
            mSprite.LoadContent(this.Content);
            
        }

        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            mSprite.Draw(this.spriteBatch);
            spriteBatch.End();

            base.Draw(gameTime);
        }

        private void Airplane_SizeChanged(object sender, EventArgs e)
        {
        }

        #region Generowane przez VS

        private System.ComponentModel.IContainer components = null;
     
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        #endregion
    }
}
